package models

import com.jfinal.plugin.activerecord.Model

class OrderM: Model<OrderM>() {

    companion object {
        val AGENT by lazy {
            OrderM()
        }
        const val TABLE_NAME = "orders"
        const val ID = "id"
        const val USER_ID = "user_id"
        const val RECORD_ID = "record_id"
        const val CREATE_TIME = "create_time"
        const val PAY_TIME = "pay_time"
        const val FINISH_TIME = "finish_time"
        const val TO_ADDR = "to_addr"
        const val STATE = "state"

        fun findOrderByIdWithUserId(orderId: Int, userId: Int): OrderM?{
            val list = AGENT.find("select * from $TABLE_NAME where $ID = ? and $USER_ID = ?", orderId, userId)
            return if (list.isNullOrEmpty()) null else list.first()
        }

        fun findOrderByUserId(userId: Int): List<OrderM>{
            return AGENT.find("select * from $TABLE_NAME where $USER_ID = ?", userId)
        }
    }
}